﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Tools;
using Tour.DAL;

namespace Tour.BLL
{
    /// <summary>
    /// Summary description for TblReservation
    /// </summary>
    public class TblReservationManagerBase
    {
        public TblReservationManagerBase()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public int InsertUpdate(TblReservationEntity item)
        {            
            List<SqlParameter> ls = new List<SqlParameter>();
            SqlParameter sp = new SqlParameter("@Output", 1);
            sp.Direction = System.Data.ParameterDirection.Output;
            ls.Add(sp);
            ls.Add(new SqlParameter("@Id", item.Id));
            ls.Add(new SqlParameter("@TextId", item.TextId));
            ls.Add(new SqlParameter("@CustomerId", item.CustomerId));
            ls.Add(new SqlParameter("@CreatedBy", item.CreatedBy));
            ls.Add(new SqlParameter("@CreatedDate", item.CreatedDate));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_InsertUpdateTblReservation", sqlparam);
            return int.Parse(sqlparam[0].Value.ToString());
        }
        public int InsertUpdate(int Id, string TextId, int CustomerId, string CreatedBy, System.DateTime CreatedDate)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            SqlParameter sp = new SqlParameter("@Output", 1);
            sp.Direction = System.Data.ParameterDirection.Output;
            ls.Add(sp);
            ls.Add(new SqlParameter("@Id", Id));
            ls.Add(new SqlParameter("@TextId", TextId));
            ls.Add(new SqlParameter("@CustomerId", CustomerId));
            ls.Add(new SqlParameter("@CreatedBy", CreatedBy));
            ls.Add(new SqlParameter("@CreatedDate", CreatedDate));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_InsertUpdateTblReservation", sqlparam);
            return int.Parse(sqlparam[0].Value.ToString());
        }
        public string GetUniqueTextIdFromUnicodeText(string UnicodeText)
        {
            string strSql = @"  SELECT TextId FROM TblReservation tn
                                WHERE tn.TextId=@TextId";
           string textId = WebUtility.GetUniqueTextIdFromUnicodeText(UnicodeText, "@TextId", strSql
               , SqlHelper.TourConnectString, 255);
           return textId;
        }
        public void Delete(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_DeleteTblReservation", sqlparam);
        }
        public TblReservationEntity GetTblReservationEntity(DataRow dr)
        {
            TblReservationEntity item = new TblReservationEntity();            
            if (dr.Table.Columns.Contains("Id"))
                item.Id = dr["Id"].ToInt();              
            if (dr.Table.Columns.Contains("TextId"))
                item.TextId = dr["TextId"].ToString();              
            if (dr.Table.Columns.Contains("CustomerId"))
                item.CustomerId = dr["CustomerId"].ToInt();              
            if (dr.Table.Columns.Contains("CreatedBy"))
                item.CreatedBy = dr["CreatedBy"].ToString();              
            if (dr.Table.Columns.Contains("CreatedDate"))
                item.CreatedDate = dr["CreatedDate"].ToDateTime();              
            return item;
        }        
        public DataTable SelectOneRDT(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            return SqlHelper.ExecuteDataTable(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_SelectTblReservation", sqlparam);
        }
        public TblReservationEntity SelectOne(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_SelectTblReservation", sqlparam);
            if(dt!=null && dt.Rows.Count>0)
                return GetTblReservationEntity(dt.Rows[0]);
            return null;
        }
        public DataTable SelectAllRDT()
        {
            return SqlHelper.ExecuteDataTable(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_SelectTblReservationsAll", null);
        }
        public List<TblReservationEntity> SelectAll()
        {
            List<TblReservationEntity> lsresult= new List<TblReservationEntity>();
            DataTable dt =  SqlHelper.ExecuteDataTable(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_SelectTblReservationsAll", null);
            if (dt != null && dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(GetTblReservationEntity(dt.Rows[i]));            
            return lsresult;
        }
    }
}

